package com.runa.monitor.platinterface.platInterface;
import com.runa.monitor.comm.dto.LegacyWsMessage;
import com.runa.monitor.platinterface.backwork.function.TaskFunction;
import com.runa.monitor.platinterface.backwork.function.WriteBack;
import com.runa.monitor.platinterface.netservice.connect.commstatus.CommStatus;
import com.runa.monitor.platinterface.netservice.equipment.Concentrator;
import com.runa.monitor.platinterface.netservice.equipment.NetEqu;
import com.runa.monitor.platinterface.platService.NetEquService;
import com.runa.persistence.SerializeUtil;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

/**
 * websocket接口
 */
@Controller
@ServerEndpoint("/websocket")
public class WebSocketInterface {

    @Resource
    private NetEquService netEquService;

    @OnOpen
    public void onOpen(Session session) {
        System.out.println(session);
    }

    @OnClose
    public void onClose(Session session) {
        System.out.println(session);
    }

    @OnMessage
    public void onMessage(String message, Session session) {
        LegacyWsMessage wsMessage = SerializeUtil.json2pojo(message, LegacyWsMessage.class);
        String buffer = ((String) wsMessage.getData().get("Buffer")).replaceAll("\\s*", "");
        String cmd = wsMessage.getCmd();
        NetEqu<String> netEqu = netEquService.getNetEquBySerialCache(wsMessage.getNetEquSerialNo());
        WriteBack<String, String> writeBack = WriteBack.createWriteBack(session);
        if ("Req_TakeAway".equals(cmd)) {
            if (netEqu != null) {
                netEqu.close();
                writeBack.writeBack(wsMessage.getNetEquSerialNo(), "成功", null);
            } else {
                writeBack.writeBack(wsMessage.getNetEquSerialNo(), null, "掉线");
            }
        } else if ("Req_SendBuffer".equals(cmd)) {
            if (netEqu instanceof Concentrator) {
                netEqu.sendMessage(new TaskFunction<Void, String, Void>() {
                    @Override
                    public void setStatus(Void o) {}
                    @Override
                    public void doApply(CommStatus status, String msg) {
                        if (CommStatus.OK.equals(status)) {
                            writeBack.writeBack(netEqu.serial(), msg, null);
                        } else {
                            writeBack.writeBack(netEqu.serial(), msg, status.getStatus());
                        }
                    }
                    @Override
                    public Void waitResult() {
                        return null;
                    }
                    @Override
                    public String getSerial() {
                        return null;
                    }

                    @Override
                    public int getPriority() {
                        return 0;
                    }
                }, buffer);
            } else if (netEqu == null) {
                writeBack.writeBack(wsMessage.getNetEquSerialNo(), null, "掉线");
            } else {
                writeBack.writeBack(wsMessage.getNetEquSerialNo(), null, "非瑞纳集中器");
            }
        }
    }

}

